package com.aidex.biz.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.aidex.biz.domain.DaibanIng;
import com.aidex.biz.domain.MonthModel;
import com.aidex.biz.service.DaibanIngService;
import com.aidex.common.annotation.DataScope;
import com.aidex.system.mapper.SysDeptMapper;
import com.github.pagehelper.PageInfo;
import com.aidex.common.core.domain.entity.SysUser;
import com.aidex.common.core.domain.entity.SysDept;
import com.aidex.common.core.service.BaseServiceImpl;
import com.aidex.framework.cache.DeptUtils;
import com.aidex.framework.cache.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.aidex.biz.mapper.YearDeptMapper;
import com.aidex.biz.domain.YearDept;
import com.aidex.biz.service.YearDeptService;
import org.springframework.transaction.annotation.Transactional;
import com.aidex.common.utils.NumberUtils;

/**
 * 年报管理Service业务层处理
 * @author sean
 * @email 2074844820@qq.com
 * @date 2025-01-22
 */
@Service
@Transactional(readOnly = true)
public class YearDeptServiceImpl extends BaseServiceImpl<YearDeptMapper, YearDept> implements YearDeptService
{
    private static final Logger log = LoggerFactory.getLogger(YearDeptServiceImpl.class);

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Autowired
    private DaibanIngService daibanIngService;

    /**
     * 获取单条数据
     * @param yearDept 年报管理
     * @return 年报管理
     */
    @Override
    public YearDept get(YearDept yearDept)
    {
        YearDept dto = super.get(yearDept);
        return dto;
    }

    /**
     * 获取单条数据
     * @param id 年报管理id
     * @return 年报管理
     */
    @Override
    public YearDept get(String id)
    {
        YearDept dto = super.get(id);
        return dto;
    }


    /**
     * 查询年报管理列表
     * @param yearDept 年报管理
     * @return 年报管理
     */
    @Override
    @DataScope(userAlias = "a",deptAlias = "a")
    public List<YearDept> findList(YearDept yearDept)
    {
        return super.findList(yearDept);
    }

    /**
     * 分页查询年报管理列表
     * @param yearDept 年报管理
     * @return 年报管理
     */
    @Override
    @DataScope(userAlias = "a",deptAlias = "a")
    public PageInfo<YearDept> findPage(YearDept yearDept)
    {
        return super.findPage(yearDept);
    }

    /**
     * 保存年报管理
     * @param yearDept
     * @return 结果
     */
    @Override
    public boolean save(YearDept yearDept)
    {
        return super.save(yearDept);
    }

    @Override
    @Transactional(readOnly = false)
    public boolean batchSave(YearDept yearDept)
    {
        SysDept sysDept=new SysDept();
        sysDept.setTreeLevel(2);
        List<SysDept> sysDeptList=sysDeptMapper.findList(sysDept);
        List<DaibanIng> daibanIngs=new ArrayList<>();
        try{
            for (SysDept dept:sysDeptList) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
                YearDept dto=new YearDept();
                dto.setTimes(sdf.parse(sdf.format(yearDept.getTimes())));
                dto.setDeptId(dept.getId());
                dto.setDeptName(dept.getDeptName());
                dto.setStatus("0");
                dto.setType("半年总结");
                super.save(dto);
                DaibanIng daibanIng=new DaibanIng();
                daibanIng.setContent("("+sdf.format(yearDept.getTimes())+")"+"半年总结");
                daibanIng.setType("半年总结");
                daibanIng.setDeptId(dto.getDeptId());
                daibanIng.setRouter("baobiao/bizYearDept");
                daibanIng.setPath("biz/monthdeptlist/index");
                daibanIng.setStatus("0");
                daibanIngs.add(daibanIng);

                YearDept dto1=new YearDept();
                dto1.setTimes(sdf.parse(sdf.format(yearDept.getTimes())));
                dto1.setDeptId(dept.getId());
                dto1.setDeptName(dept.getDeptName());
                dto1.setType("年度总结");
                dto1.setStatus("0");
                super.save(dto1);
                DaibanIng daibanIng1=new DaibanIng();
                daibanIng1.setContent("("+sdf.format(yearDept.getTimes())+")"+"年度总结");
                daibanIng1.setType("年度总结");
                daibanIng1.setDeptId(dto1.getDeptId());
                daibanIng1.setRouter("baobiao/bizYearDept");
                daibanIng1.setPath("biz/yeardept/index");
                daibanIng1.setStatus("0");
                daibanIngs.add(daibanIng1);
            }
            daibanIngService.insertBatch(daibanIngs);
        }catch (Exception ex){
            return false;
        }

        return true;
    }


    /**
     * 删除年报管理信息
     * @param yearDept
     * @return 结果
     */
    @Override
    public boolean remove(YearDept yearDept)
    {
        return super.remove(yearDept);
    }

    /**
     * 批量删除年报管理
     * @param ids 需要删除的年报管理ID
     * @return 结果
     */
    @Transactional(readOnly = false)
    @Override
    public int deleteYearDeptByIds(String[] ids)
    {
        return mapper.deleteYearDeptByIds(ids);
    }

    /**
     * 获取最大编号
     * @param yearDept 年报管理
     * @return 结果
     */
    @Override
    public int findMaxSort(YearDept yearDept)
    {
        return NumberUtils.nextOrder(mapper.findMaxSort(yearDept));
    }
}
